package com.yuand.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.yuand.entity.Commission;
import com.yuand.interceptor.MySheetWriteHandler;
import com.yuand.mapper.CommissionMapper;
import com.yuand.utils.Result;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ProjectName: boot_easyexcel
 * @PackageName: com.yuand.controller
 * @ClassName: ExcelExport
 * @Author: yuand
 * @Date 2021/9/30 15:16
 * @Week: 星期四
 * @Description: 导出excel
 */

@RestController
@RequestMapping("/excel")
public class ExcelExport {

    @Autowired
    private CommissionMapper commissionMapper;



    @GetMapping("/export")
    public void excelExport (HttpServletResponse response) throws IOException {
        List<Commission> commissionList = this.commissionMapper.selectList(null);

        //设置序号
        for (int i = 1;i <= commissionList.size();i++){
            commissionList.get(i-1).setOrderNum(i+"");
        }

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("提成计算统计表", "UTF-8");
        response.setHeader("Content-disposition",  "attachment;filename=" + fileName + ".xls");


        //内容样式策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //垂直居中,水平居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        //设置 自动换行
        contentWriteCellStyle.setWrapped(true);
        // 字体策略
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 12);
        contentWriteCellStyle.setWriteFont(contentWriteFont);


        //头策略使用默认
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();

        //excel如需下载到本地,只需要将response.getOutputStream()换成File即可(注释掉以上response代码)
        EasyExcel.write(response.getOutputStream(), Commission.class)

                //设置输出excel版本,不设置默认为xlsx
                .excelType(ExcelTypeEnum.XLS).head(Commission.class)
                //设置拦截器或自定义样式
                .registerWriteHandler(new MySheetWriteHandler())

                .registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle))
                .sheet("提成计算统计表")
                //设置默认样式及写入头信息开始的行数
                .useDefaultStyle(true).relativeHeadRowIndex(2)
                //这里的addsumColomn方法是个添加合计的方法,可删除
                .doWrite(commissionList);

    }

}
